package com.gowants.pems.controller;

import java.util.Properties;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.gowants.pems.aspect.Importance;
import com.gowants.pems.aspect.OperType;
import com.gowants.pems.aspect.ScmsAopLog;
import com.gowants.pems.config.AccessLimit;
import com.gowants.pems.model.Sys;
import com.gowants.pems.service.SysService;
import com.gowants.pems.util.DateUtil;
import com.gowants.pems.util.DownLoadUtil;
import com.gowants.pems.util.JSONMap;

@RestController
@RequestMapping(value = "/sys/")
public class SysController extends BaseController {

	@Autowired
	private SysService SysService;

	/**
	 * 获取系统信息
	 * 
	 * @return
	 */
	@RequestMapping(value = "getSys")
	@AccessLimit(seconds = 1, maxCount = 3) //1秒内 允许请求3次
	public JSONMap getSys(String reqData, HttpServletRequest req) {
		String code = "";
		try {
			JSONMap response = new JSONMap();
			Sys record = SysService.selectOne("0");
			Properties props = System.getProperties();
			String os = props.getProperty("os.name");
			String version = props.getProperty("os.version");
			response.put("sys", record);
			response.put("os", os);
			response.put("version", version);
			return doResponse(req, code, response);

		} catch (Exception e) {
			return doResponse(req, e.getMessage());
		}

	}
	
	/**
	 * 
	 * <编辑>修改
	 * 
	 * @param request
	 * @param request
	 * @return
	 * @remake
	 */
	@RequestMapping(value = "update")
	@AccessLimit(seconds = 1, maxCount = 3) //1秒内 允许请求3次
	@ScmsAopLog(description = "系统信息修改", opType = OperType.execute, importance = Importance.commonly)
	public JSONMap update(String request, HttpServletRequest req) {
		String code = "TSMS_0000047";

		Sys record = getRequestBody(request, Sys.class);
		record.setId("0");
		record.setUpdateTime(DateUtil.getDate());
		int executeNum = this.SysService.update(record);

		if (executeNum > 0) {
			code = isClient(req) ? "" : "TSMS_0000002";
		}

		return doResponse(req, code, null);
	}

	/**
	 * <下载>
	 *
	 * @param
	 * @return
	 * @remake
	 */
	@RequestMapping(value = "download")
	@AccessLimit(seconds = 1, maxCount = 3) //1秒内 允许请求3次
	public void download(String accessoriesId, HttpServletResponse resp) {
		String filePath = SysService.getSaveAddress("app_download");

		try {
			DownLoadUtil.downLoad(filePath, resp, false);
		} catch (Exception e) {
		}
	}

}
